//
//  CLHViewController.m
//  ListViewModel
//
//  Created by clh2046@163.com on 07/29/2019.
//  Copyright (c) 2019 clh2046@163.com. All rights reserved.
//

#import "CLHViewController.h"
#import <ListViewModel/ListViewModel.h>
#import "CLHViewControllerViewModel.h"
#import "CLHTableViewVC.h"
#import "CLHCollectionViewVC.h"
#import "ListViewModel_Example-Swift.h"

#define WEAK_SELF __weak typeof(self)weakSelf = self;
#define STRONG_SELF __strong typeof(weakSelf)self = weakSelf;

@interface CLHViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tabliView;
@property (nonatomic, strong) CLHViewControllerViewModel *viewModel;

@end

@implementation CLHViewController
-(CLHViewControllerViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[CLHViewControllerViewModel alloc]init];
    }
    return _viewModel;
}
- (UITableView *)tabliView {
    if (!_tabliView) {
        _tabliView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        _tabliView.delegate = self;
        _tabliView.dataSource = self.viewModel.dataSource;
        _tabliView.estimatedRowHeight = 120;
        _tabliView.rowHeight = UITableViewAutomaticDimension;
        _tabliView.backgroundColor = [UIColor whiteColor];
        _tabliView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    return _tabliView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tabliView];
    [self.viewModel.dataSource resetDataSource:self];
    WEAK_SELF
    self.viewModel.reloadTableViewBlock = ^(NSIndexSet * _Nullable sections, NSArray<NSIndexPath *> * _Nullable indexPaths) {
        STRONG_SELF
        if (sections) {
            [self.tabliView reloadSections:sections withRowAnimation:UITableViewRowAnimationNone];
        } else if(indexPaths) {
            [self.tabliView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
        } else {
            [self.tabliView reloadData];
        }
    };
    self.viewModel.showLoadingBlock = ^(NSString * _Nullable message) {
        NSLog(@"showLoadingBlock = %@",message);
    };
    self.viewModel.hideLoadingBlock = ^(NSString * _Nullable message) {
        NSLog(@"hideLoadingBlock = %@",message);
    };
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.viewModel didSelectedRowModelWithIndexPath:indexPath];
    if (indexPath.row == 0) {
        [self.navigationController pushViewController:[CLHTableViewVC new] animated:YES];
    } else if (indexPath.row == 1) {
        [self.navigationController pushViewController:[CLHCollectionViewVC new] animated:YES];
    } else if (indexPath.row == 2) {

        [self.navigationController pushViewController:[CLHSwiftCodeVC new] animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.viewModel.dataSource rowModelAtIndex:indexPath].rowHeight;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self.viewModel reloadTableView];
}

@end
